package com.malex.mxtracker.component;

import com.malex.mxtracker.MxTracker;
import com.malex.mxtracker.action.ActionHandler;
import com.malex.mxtracker.action.ActionResult;
import com.malex.mxtracker.bean.Project;
import com.malex.mxtracker.exception.WrongCommandException;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;

/**
 * Homepage
 */
public class HomePage extends WebPage {
    private Project selected = null;
    private static final long serialVersionUID = 1L;

    public HomePage(final PageParameters parameters) {
        String sProject;
        if (parameters != null && parameters.containsKey("project")) {
            sProject = (String) parameters.get("project");
            setSelected(MxTracker.getProjects().get(Long.valueOf(sProject)));
        }
        MxTracker.updateData();
        createView();
    }

    private void createView() {
        add(new CurrenciesPanel("currencies"));
        add(new CommandForm("form"));
        add(new BanksPanel("banks"));
        add(new OperationsPanel("operations", getSelected()));
        add(new ProjectsPanel("projects", getSelected()));
        add(new FeedbackPanel("feedback"));
    }

    public Project getSelected() {
        return selected;
    }

    public void setSelected(final Project selected) {
        this.selected = selected;
    }

    private class CommandForm extends Form {
        private String command;

        public CommandForm(String id) {
            super(id);
            TextField<String> stringTextField = new TextField<String>("command",
                    new PropertyModel<String>(this, "command"));
            add(stringTextField);
        }

        @Override
        protected void onSubmit() {
            ActionResult result = null;
            try {
                result = new ActionHandler(getCommand()).execute();
                StringBuffer sb = new StringBuffer();
                if (result.getReport() != null) {
                    for (String s : result.getReport()) {
                        sb.append(s).append("\n");
                    }
                    info(sb.toString());
                }
            } catch (WrongCommandException e) {
                warn(e.getMessage());
                e.printStackTrace();
            }
            PageParameters pageParameters = new PageParameters();
            if (result != null && result.getPesponseParams() != null) {
                for (String key : result.getPesponseParams().keySet()) {
                    pageParameters.put(key, result.getPesponseParams().get(key));
                }
            }
            setResponsePage(new HomePage(pageParameters));
        }

        public String getCommand() {
            return command;
        }

        public void setCommand(final String command) {
            this.command = command;
        }
    }
}
